﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ChuanYe.WorkLogManagement.Api.Models;
using ChuanYe.WorkLogManagement.BLL;
using ChuanYe.WorkLogManagement.EF.SqlServer;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ChuanYe.WorkLogManagement.Api.Controllers
{
    /// <summary>
    /// 职务接口
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class PositionController : ControllerBase
    {
        /// <summary>
        /// 增加或编辑职务
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<int> AddOrUpdate(PositionRequest obj)
        {
            ResultDto<int> result = new ResultDto<int>();
            try
            {
                PositionInfo info = new PositionInfo()
                {
                    PositionID = obj.PositionID,
                    PositionName = obj.PositionName,
                    Sort=obj.Sort,
                };
                int positionID = PositionBLL.Instance.AddOrUpdate(info);
                if (positionID == -1)
                {
                    result.FirstParam = -1;
                    result.Status = 0;
                    result.Message = "Update Target PositionID Not Exist";
                }
                else
                {
                    result.FirstParam = positionID;
                    result.Status = 1;
                    result.Message = "Requst Is Success";
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Position/AddOrUpdate Server Error";
                LogHelper.Error(ex.Message);
            }
            return result;
        }

        /// <summary>
        ///  移除单条职务
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDtoBase Remove(DelPositionRequest obj)
        {
            ResultDtoBase result = new ResultDtoBase();
            try
            {
                var delRow = PositionBLL.Instance.Remove(obj.PositionID);
                if (delRow == 0)
                {
                    result.Status = 0;
                    result.Message = "Update Target GUID Not Exist";
                }
                else
                {
                    result.Status = 1;
                    result.Message = "Requst Is Success";
                }
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Work/Remove Server Error";
                LogHelper.Error(ex.Message);
            }
            return result;
        }

        /// <summary>
        ///  获取职务列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ResultDto<dynamic> Get(DelPositionRequest obj)
        {
            ResultDto<dynamic> result = new ResultDto<dynamic>();
            try
            {
                var list = PositionBLL.Instance.Get();
                result.FirstParam = list;
                result.Status = 1;
                result.Message = "Requst Is Success";
            }
            catch (Exception ex)
            {
                result.Status = 500;
                result.Message = "/Work/Remove Server Error";
                LogHelper.Error(ex.Message);
            }
            return result;
        }

    }
}